package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.model.hosp.Department;
import com.atguigu.yygh.common.model.hosp.Hospital;
import com.atguigu.yygh.common.model.hosp.HospitalSet;
import com.atguigu.yygh.common.model.hosp.Schedule;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.common.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.common.vo.hosp.ScheduleOrderVo;
import com.atguigu.yygh.common.vo.hosp.ScheduleQueryVo;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.utiles.SignKey;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author LY
 * 医院模拟操作系统所需要的接口
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiManageController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;


    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = SignKey.checkSignKey(request);
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

    @ApiOperation(value = "分页显示排班")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = SignKey.checkSignKey(request);
       int page  = StringUtils.isEmpty(paramMap.get("page")) ? 1 :Integer.parseInt((String) paramMap.get("page"));
       int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1: Integer.parseInt((String) paramMap.get("limit"));
       ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
       Page<Schedule> queryVoPage = scheduleService.findPage(page,limit,scheduleQueryVo);
       return Result.ok(queryVoPage);
    }

    @ApiOperation(value = "上传排班")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传递过来的排班信息并进行数据校验
        Map<String, Object> paramMap = SignKey.checkSignKey(request);
        //执行保存
        scheduleService.saveSchedule(paramMap);
        return Result.ok();
    }


    /**
     * 删除科室接口
     * @param request
     * @return
     */
    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //1. 获取传递过来的科室信息并进行数据校验
        Map<String, Object> map = SignKey.checkSignKey(request);
        //2.获取医院编号以及科室编号
        String hoscode = (String)map.get("hoscode");
        String depcode = (String)map.get("depcode");
        //3. 执行删除
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    /**
     * 查询科室接口
     * @param request
     * @return
     */
    @ApiOperation(value = "查询科室")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //1.获取传递过来的科室信息并进行数据校验
        Map<String, Object> map = SignKey.checkSignKey(request);
        //2. 获取page,limit,查询条件
        int page = StringUtils.isEmpty(map.get("page"))   ? 1 : Integer.parseInt((String)map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit")) ? 1 : Integer.parseInt((String)map.get("limit"));
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        //3. 查询出结果并分页
        Page<Department> departments =  departmentService.findPage(page,limit,departmentQueryVo);
        return Result.ok(departments);
    }
    /**
     * 上传科室接口
     * @param request
     * @return
     */
    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //1. 获取传递过来的科室信息并进行签名校验
        Map<String, Object> paramMap = SignKey.checkSignKey(request);
        //2. 上传科室
        departmentService.save(paramMap);
        return Result.ok();
    }


    /**
     * 查询医院接口
     * @param request
     * @return
     */
    @PostMapping("hospital/show")
    public Result getByHoscode(HttpServletRequest request){
        //获取传递过来的医院信息并进行签名校验
        Map<String, Object> map = SignKey.checkSignKey(request);
        String hoscode = (String) map.get("hoscode");
        //调用方法
        Hospital hospital  =  hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){

        //1. 获取传递过来的医院信息并进行签名校验
        Map<String, Object> paramMap = SignKey.checkSignKey(request);

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)paramMap.get("logoData");
        if (!StringUtils.isEmpty(logoData)){
            String s = logoData.replaceAll(" ", "+");
            paramMap.put("logoData",s);
        }

        //调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }
}
